﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;
using SQLServerHealthCheck.SQLServer.AutoFixes;
using System.Windows.Media;

namespace PlayGround.WPF
{
    class fixer
    {
        public List<Issue> checkedissues = new List<Issue>();

        public fixer() { }

        public void getAllCheckedIssues(IssueViewModel current)
        {
            if (current.Children.Count() != 0)
            {
                foreach (IssueViewModel item in current.Children)
                {
                    getAllCheckedIssues(item);

                }
            }
            else
            {
                if (current.Issue != null)
                {
                    if ((bool)current.IsChecked)
                    {
                        checkedissues.Add(current.Issue);
                        current.IsChecked = false;
                        

                    }

                }


            }

        }

        public void fixissues(IssueViewModel current)
        {


            getAllCheckedIssues(current);


            List<List<Issue>> toFix = new List<List<Issue>>();
            IssueType it = null;
            List<Issue> sameIssues = new List<Issue>();
            foreach (Issue item in checkedissues)
            {


                if (it == null)
                {


                    it = item.IssueType;
                    sameIssues.Add(item);

                }
                else if (item.IssueType.IssueTypeId == it.IssueTypeId)
                {
                    sameIssues.Add(item);
                }
                else
                {
                    toFix.Add(sameIssues);
                    it = item.IssueType;
                    sameIssues = new List<Issue>(); ;
                    sameIssues.Add(item);


                }

            }
            List<AutoFixBase> fixes = AutoFixFactory.GetAllFixes();
            if (sameIssues.Count != 0)
            { toFix.Add(sameIssues); }
            foreach (var issuelist in toFix)
            {

                foreach (var fix in fixes)
                {
                    if (issuelist[0].IssueTypeId == fix.IssueTypeId)
                    {
                        List<Issue> fgikj = new List<Issue>();
                        foreach (var issue in issuelist)
                        {
                            fgikj.Add(issue);
                        }
                        fix.Fix(fgikj);
                    }
                }

            }


        }



        //    List<List<Issue>> toFix = new List<List<Issue>>();
        //    IssueType it = null;
        //    List<Issue> sameIssues = new List<Issue>();
        //    foreach (Issue item in listBox1.Items)
        //    {

        //        ListBoxItem myListBoxItem = (ListBoxItem)(listBox1.ItemContainerGenerator.ContainerFromItem(item));
        //        ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
        //        DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
        //        System.Windows.Controls.CheckBox target = (System.Windows.Controls.CheckBox)myDataTemplate.FindName("CheckBoxZone", myContentPresenter);

        //        if (/*item.IssueType.Fixable == true &&*/ target.IsChecked == true)
        //        {
        //            if (it == null)
        //            {


        //                it = item.IssueType;
        //                sameIssues.Add(item);

        //            }
        //            else if (item.IssueType.IssueTypeId == it.IssueTypeId)
        //            {
        //                sameIssues.Add(item);
        //            }
        //            else
        //            {
        //                toFix.Add(sameIssues);
        //                it = item.IssueType;
        //                sameIssues = new List<Issue>(); ;
        //                sameIssues.Add(item);


        //            }
        //        }
        //    }
        //    List<AutoFixBase> fixes = AutoFixFactory.GetAllFixes();
        //    if (sameIssues.Count != 0)
        //    { toFix.Add(sameIssues); }
        //    foreach (var issuelist in toFix)
        //    {

        //        foreach (var fix in fixes)
        //        {
        //            if (issuelist[0].IssueTypeId == fix.IssueTypeId)
        //            {
        //                List<Issue> fgikj = new List<Issue>();
        //                foreach (var issue in issuelist)
        //                {
        //                    fgikj.Add(issue);
        //                }
        //                fix.Fix(fgikj);
        //            }
        //        }

        //    }
        //    issues.Clear();
        //    var newissues = from fx in dc.Issues
        //                    where fx.Fixed != true || fx.Fixed == null
        //                    && fx.IssueType.Fixable == true
        //                    select fx;
        //    foreach (var item in newissues)
        //    {
        //        if (item.IssueType.Fixable == true)
        //            issues.Add(item);
        //    }
        //    listBox1.ItemsSource = null;
        //    listBox1.ItemsSource = issues;

        //}

    }
}
